package com.datatools.ui.frame;

import com.datatools.entity.ConnectionInfoEntity;
import com.datatools.entity.DataBaseTypeEnum;
import com.datatools.ui.dialog.MsgDialog;
import com.datatools.util.StringUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

/**
 * 创建数据库连接dialog
 */
public class CreateConnectionDialog extends JDialog implements Serializable {
    private static final long serialVersionUID = 1L;

    //消息展示
    private JLabel msgLable = new JLabel();

    private JLabel conNameLabel = new JLabel("自定义名称");
    private JTextField conNameTxt = new JTextField("新连接");

    private JLabel dbTypeLabel = new JLabel("数据库类型:");
    private JComboBox dbTypeComboBox = new JComboBox();

    private JLabel ipLabel = new JLabel("IP:");
    private JTextField ipTxt = new JTextField("127.0.0.1");

    private JLabel portLabel = new JLabel("端口:");
    private JTextField portTxt = new JTextField("3306");

    private JLabel dbNameLabel = new JLabel("数据库名:");
    private JTextField dbNameTxt = new JTextField("test");

    private JLabel userNameLabel = new JLabel("用户名:");
    private JTextField userNameTxt = new JTextField("root");

    private JLabel passwordLabel = new JLabel("密码:");
    private JPasswordField passwordTxt = new JPasswordField();

    private JButton conBtn = new JButton("创建");
    private JButton testConBtn = new JButton("测试连接");

    private String conName;
    private DataBaseTypeEnum dbType;
    private String ip;
    private String port;
    private String userName;
    private String password;
    private String dbName;

    private ConnectionInfoEntity connectionInfoEntity;

    public CreateConnectionDialog() {
        this.setModal(true);//模态窗口

        this.setName("新建数据库连接");
        this.setTitle("新建数据库连接");
        this.setLayout(null);
        this.setSize(500, 500);
        this.setResizable(false);
        this.setLocationRelativeTo(null);//窗体居中显示

        int labelX = 70;
        int labelWidth = 100;
        //行的Y坐标 递增 50
        int rowY = 10;
        //行高
        int rowHeight = 35;
        int txtX = labelX + labelWidth + 10;
        int txtWidth = 250;


        //自定义连接名称
        conNameLabel.setBounds(labelX, rowY, labelWidth, rowHeight);
        conNameTxt.setBounds(txtX, rowY, txtWidth, rowHeight);
        this.add(conNameLabel);
        this.add(conNameTxt);
        rowY += 50;

        //初始化数据类型的选择
        dbTypeComboBox.addItem(DataBaseTypeEnum.MYSQL);
        dbTypeComboBox.addItem(DataBaseTypeEnum.SQLSERVER);
        dbTypeComboBox.addItem(DataBaseTypeEnum.ORACLE);

        //数据库类型
        dbTypeLabel.setBounds(labelX, rowY, labelWidth, rowHeight);
        dbTypeComboBox.setBounds(txtX, rowY, txtWidth, rowHeight);
        this.add(dbTypeLabel);
        this.add(dbTypeComboBox);
        rowY += 50;
        //IP
        ipLabel.setBounds(labelX, rowY, labelWidth, rowHeight);
        ipTxt.setBounds(txtX, rowY, txtWidth, rowHeight);
        this.add(ipLabel);
        this.add(ipTxt);
        rowY += 50;

        //端口
        portLabel.setBounds(labelX, rowY, labelWidth, rowHeight);
        portTxt.setBounds(txtX, rowY, txtWidth, rowHeight);
        this.add(portLabel);
        this.add(portTxt);
        rowY += 50;

        //数据库名称
        dbNameLabel.setBounds(labelX, rowY, labelWidth, rowHeight);
        dbNameTxt.setBounds(txtX, rowY, txtWidth, rowHeight);
        this.add(dbNameLabel);
        this.add(dbNameTxt);
        rowY += 50;

        userNameLabel.setBounds(labelX, rowY, labelWidth, rowHeight);
        userNameTxt.setBounds(txtX, rowY, txtWidth, rowHeight);
        this.add(userNameLabel);
        this.add(userNameTxt);
        rowY += 50;


        passwordLabel.setBounds(labelX, rowY, labelWidth, rowHeight);
        passwordTxt.setBounds(txtX, rowY, txtWidth, rowHeight);
        this.add(passwordLabel);
        this.add(passwordTxt);
        rowY += 50;

        testConBtn.setBounds(txtX, rowY, 100, rowHeight);
        this.add(testConBtn);

        conBtn.setBounds(txtX + 120, rowY, 100, rowHeight);
        this.add(conBtn);

        rowY += 50;
        msgLable.setForeground(Color.red);
        msgLable.setBounds(txtX,rowY,300,rowHeight);
        this.add(msgLable);

        //测试连接
        testConBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                testConnect();
            }
        });

        //创建连接
        conBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (testConnect()) {
                    //关闭Dialog
                    CreateConnectionDialog.this.dispose();
                }
            }
        });
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }

    /**
     * 测试连接
     *
     * @return
     */
    public boolean testConnect() {
        if (null != this.connectionInfoEntity) {
            this.connectionInfoEntity.closeConnection();
        }
        this.connectionInfoEntity = null;

        if (validFields()) {
            this.connectionInfoEntity = new ConnectionInfoEntity();
            this.connectionInfoEntity.setConName(getConName());
            this.connectionInfoEntity.setDbType(getDbType());
            this.connectionInfoEntity.setIp(getIp());
            this.connectionInfoEntity.setPort(getPort());
            this.connectionInfoEntity.setUserName(getUserName());
            this.connectionInfoEntity.setPassword(getPassword());
            this.connectionInfoEntity.setDbName(getDbName());

            if (null != this.connectionInfoEntity.getConnection()) {
                return true;
            }
            if (null != connectionInfoEntity.getErrorSb()) {
                MsgDialog.show(connectionInfoEntity.getErrorSb().toString());
            }
        }
        this.connectionInfoEntity = null;
        return false;
    }

    public boolean validFields() {

        msgLable.setText("");

        conName = conNameTxt.getText();
        if (StringUtils.isNullOrEmpty(conName)) {
            msgLable.setText("请定义连接名称");

            return false;
        }
        //TODO 检查连接名称是否已存在

        dbType = (DataBaseTypeEnum) dbTypeComboBox.getSelectedItem();

        ip = ipTxt.getText();
        if (StringUtils.isNullOrEmpty(ip)) {
            msgLable.setText("IP不能为空");

            return false;
        }
        port = portTxt.getText();
        if (StringUtils.isNullOrEmpty(port)) {
            msgLable.setText("端口不能为空");

            return false;
        }
        userName = userNameTxt.getText();
        if (StringUtils.isNullOrEmpty(userName)) {
            msgLable.setText("用户名不能为空");

            return false;
        }
        password = passwordTxt.getText();
        if (StringUtils.isNullOrEmpty(password)) {
            msgLable.setText("密码不能为空");

            return false;
        }
        dbName = dbNameTxt.getText();
        if (StringUtils.isNullOrEmpty(dbName)) {
            msgLable.setText("数据库不能为空");

            return false;
        }
        return true;
    }

    public String getConName() {
        return conName;
    }

    public void setConName(String conName) {
        this.conName = conName;
    }

    public DataBaseTypeEnum getDbType() {
        return dbType;
    }

    public void setDbType(DataBaseTypeEnum dbType) {
        this.dbType = dbType;
    }

    public ConnectionInfoEntity getConnectionInfoEntity() {
        return connectionInfoEntity;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }
}
